package com.example.scaffolding.ui.user

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.scaffolding.R
import com.example.scaffolding.db.DataBaseInstance
import com.example.scaffolding.db.MyDataBase
import com.example.scaffolding.db.bean.User
import com.example.scaffolding.db.dao.UserDao

class DBFragment : Fragment() {
    /**
     * show ui by RecyclerView
     */
    private var mRecyclerView: RecyclerView? = null

    /**
     * Adapter
     */
    private var adapterCommon: RecyclerAdapterCommon? = null

    /**
     * Dao
     */
    var userDao: UserDao? = null

    /**
     * Roomdatabase
     */
    private var myDateBase: MyDataBase? = null

    /**
     * VM
     */
    var userViewModel: UserViewModel? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_db, container, false)
        mRecyclerView = view.findViewById(R.id.recyclerview)
        init()
        mockData()
        return view
    }

    override fun onDestroy() {
        super.onDestroy()
        DataBaseInstance.setINSTANCEtoNull() //release the context
    }

    /**
     * init the variates
     */
    private fun init() {
        myDateBase = DataBaseInstance.getINSTANCE(activity!!.applicationContext)
        userDao = myDateBase?.userDao()
        adapterCommon = RecyclerAdapterCommon()
        userViewModel =
            ViewModelProvider(this, object : AndroidViewModelFactory(activity!!.application) {
                override fun <T : ViewModel?> create(modelClass: Class<T>): T {
                    return UserViewModel(userDao) as T
                }
            }).get(UserViewModel::class.java)
        userViewModel!!.users.observe(this, Observer { users: List<User> ->
            adapterCommon!!.setmUsers(users)
            adapterCommon!!.notifyDataSetChanged()
        })
        mRecyclerView!!.adapter = adapterCommon
        mRecyclerView!!.layoutManager =
            LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        mRecyclerView!!.addItemDecoration(DividerItemDecoration(activity, RecyclerView.VERTICAL))
    }

    /**
     * inflate the data
     */
    private fun mockData() {
        for (i in 0..39) {
            val user = User()
            user.name = "张三$i"
            user.type = i * 10
            user.url = "www.ddd.com$i"
            userDao!!.insert(user)
        }
    }

    companion object {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private const val ARG_SECTION_NUMBER = "section_number"

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        @JvmStatic
        fun newInstance(sectionNumber: Int): DBFragment {
            return DBFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }
    }



}